//
//  AdoptedTrusteeCell.m
//  PigMarket
//
//  Created by mac on 2019/5/14.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AdoptedTrusteeCell.h"

@interface AdoptedTrusteeCell ()

/**
 bgImg
 */
@property (nonatomic , strong) UIImageView *bgImg;
/**
 chect
 */
@property (nonatomic , strong) UIImageView *checkImg;
/**
 price
 */
@property (nonatomic , strong) UILabel *priceLab;
/**
 title
 */
@property (nonatomic , strong) UILabel *title;
/**
 days
 */
@property (nonatomic , strong) UILabel *days;
/**
 优惠
 */
@property (nonatomic , strong) UILabel *discounts;
/**
 选中icon
 */
@property (nonatomic , strong) UIButton *selectIcon;


@end
@implementation AdoptedTrusteeCell
#pragma mark - init method

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.bgImg];
    [self.bgImg addSubview:self.checkImg];
    [self.bgImg addSubview:self.priceLab];
    [self.bgImg addSubview:self.title];
    [self.bgImg addSubview:self.days];
    [self.bgImg addSubview:self.selectIcon];
    [self.bgImg addSubview:self.discounts];
}

- (void)initLayout {
    // 布局子控件
    [self.bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(106, 125));
    }];
    [self.checkImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-18);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(14);
        make.right.mas_equalTo(-10);
    }];
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.priceLab.mas_bottom).offset(4);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
    [self.discounts mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.title.mas_bottom).offset(10);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(0);
    }];
    [self.days mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-20);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
}

- (void)configCellWithModel:(AdoptedTrustee *)model{
    self.priceLab.text = [NSString stringWithFormat:@"￥%.2f",[model.money floatValue]];
    self.title.text = model.name;
    if ([model.status intValue] == 0) {//可选
        
        if ([model.type intValue] == TrusteePriceTypeInitial) {//如果为可选状态  且 为仔猪价格   则默认选中状态
            self.bgImg.image = [UIImage imageNamed:@"adopt_adoptdetail_trustee_selected"];
            self.checkImg.image = [UIImage imageNamed:@"adopt_buy_selected"];
            model.isSelect = YES;
        }
        if (model.isSelect) {//绿框显示状态
            self.bgImg.image = [UIImage imageNamed:@"adopt_adoptdetail_trustee_selected"];
            self.checkImg.image = [UIImage imageNamed:@"adopt_buy_selected"];
        }else {
            self.bgImg.image = [UIImage imageNamed:@"adopt_adoptdetail_trustee_default"];
            self.checkImg.image = [UIImage imageNamed:@""];
        }
    } else if ([model.status intValue] == 1){//置灰  不可选
        self.bgImg.image = [UIImage imageNamed:@"adopted_bg_unableSelect"];
        self.checkImg.image = [UIImage imageNamed:@"adopted_icon_unableSelect"];
    }
    
    
    if ([model.type intValue] == TrusteePriceTypeInitial) {//仔猪价格
        
        self.days.hidden = YES;
        self.discounts.hidden = YES;
    } else if ([model.type intValue] == TrusteePriceTypeAll){//全包
        self.days.hidden = NO;
        self.discounts.hidden = NO;
        self.discounts.text = [NSString stringWithFormat:@"可优惠%@元",model.precentMoney];
    }else {
        self.days.hidden = NO;
        self.discounts.hidden = YES;
    }
    self.days.text = [NSString stringWithFormat:@"%@天",model.day];
}

- (void)selectItem:(UIButton *)button {
    button.selected = !button.selected;
}
#pragma mark - getter and setter
- (UIImageView *)bgImg {
    if (!_bgImg) {
        _bgImg = [[UIImageView alloc] init];
    }
    return _bgImg;
}
- (UIImageView *)checkImg {
    if (!_checkImg) {
        _checkImg = [[UIImageView alloc] init];
    }
    return _checkImg;
}
//- (UIButton *)bgBtn {
//    if (!_bgBtn) {
//        _bgBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
//        _bgBtn.adjustsImageWhenHighlighted = NO;
//        [_bgBtn setBackgroundImage:[UIImage imageNamed:@"adopt_adoptdetail_trustee_default"] forState:UIControlStateNormal];
//        [_bgBtn setBackgroundImage:[UIImage imageNamed:@"adopt_adoptdetail_trustee_selected"] forState:UIControlStateSelected];
//        [_bgBtn addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventTouchUpInside];
//        [_bgBtn setImage:[UIImage imageNamed:@"adopt_buy_selected"] forState:UIControlStateSelected];
//        [_bgBtn setImageEdgeInsets:UIEdgeInsetsMake(30, 30, -30, -30)];
//    }
//    return _bgBtn;
//}
- (UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] init];
        _priceLab.textColor = [UIColor colorWithHexString:@"#F25C62"];
        _priceLab.font = [UIFont smallFont];
    }
    return _priceLab;
}
- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.textColor = [UIColor colorWithHexString:@"#6B6B6B"];
        _title.font = [UIFont systemFontOfSize:13];
        _title.numberOfLines = 0;
    }
    return _title;
}
- (UILabel *)days {
    if (!_days) {
        _days = [[UILabel alloc] init];
        _days.textColor = [UIColor colorWithHexString:@"#999999"];
        _days.font = [UIFont smallFont];
    }
    return _days;
}
- (UIButton *)selectIcon {
    if (!_selectIcon) {
        _selectIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectIcon setBackgroundImage:[UIImage imageNamed:@"adopt_buy_selected"] forState:UIControlStateSelected];
        [_selectIcon setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        
        [_selectIcon addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _selectIcon;
}
- (UILabel *)discounts {
    if (!_discounts) {
        _discounts = [[UILabel alloc] init];
        _discounts.textColor = [UIColor colorWithHexString:@"FF8A00"];
        _discounts.font = [UIFont systemFontOfSize:10];
        _discounts.numberOfLines = 0;
    }
    return _discounts;
}
@end
